package com.neuedu.autosale.sys.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.neuedu.autosale.entity.Pager;
import com.neuedu.autosale.entity.Result;
import com.neuedu.autosale.sys.entity.Storage;
import com.neuedu.autosale.sys.service.StorageService;


@Controller
public class StorageController {
	
	@Autowired
	private StorageService storageService;
	
	@RequestMapping("storage/index")
	public String storageIndex() {
		return "storage/storagelist";
	}
	
	@RequestMapping("storage/findall")
	@ResponseBody
	public List<Storage> Findall(){
		return storageService.findAll();
	}
	
	@RequestMapping("storage/list")
	@ResponseBody
	public Pager<Storage> storagelist(@RequestParam(value = "pager",defaultValue = "1") Integer page,
			@RequestParam(value = "rows",defaultValue = "10") Integer rows,HttpServletRequest request){
		String storageString = request.getParameter("storageName");
		Storage storageName = new Storage(storageString);
//		Pager<Storage> pager = new Pager<>(page,rows);
		Pager<Storage> pager = new Pager<Storage>(page, rows, storageName);
		List<Storage> storagelist = storageService.findListByPager(pager);
		int total = storageService.findTotalByPager(pager);
		pager.setRows(storagelist);
		pager.setTotal(total);
		return pager;
	}
	
	@RequestMapping("storage/add")
	@ResponseBody
	public Result doAdd(Storage storage) {
		Result result = storageService.add(storage);
		return result;
	}
	
	@RequestMapping("storage/edit")
	@ResponseBody
	public Result doEdit(Storage storage) {
		Result result = storageService.edit(storage);
		return result;
	}
	
	@RequestMapping("storage/delete")
	@ResponseBody
	public Result doDelete(Integer storageId) {
		List<Storage> list = new ArrayList<Storage>();
		list.add(new Storage(storageId));
		Result result = storageService.deleteByIds(list);
		return result;
	}

}
